layui.use(['jquery','table','layer','laydate'],function () {
    var $ = layui.jquery;
    var table = layui.table;
    var layer = layui.layer;
    var laydate = layui.laydate;

    laydate.render({
        elem:"#createDate",
        type:"date",
        tigger:"click"
    });

    var userIdd = localStorage.getItem("userId");
    var trueName = localStorage.getItem("trueName");
    var account = localStorage.getItem("account");
    var userName = localStorage.getItem("userName");

    selectNoUser("","","",0);
    function selectNoUser(account,nickName,createDate,consumption) {
        table.render({
            elem:"#root",
            url:"/beforeUser/selectZeroUser?account="+account+"&nickName="+nickName+"&createDate="+createDate+"&consumption"+consumption,
            defaultToolbar: ['','',''],
            cellHeight:57,
            page:true,
            limit:10,
            limits:[10,15,20],
            size: 'lg',
            cols:[[
                {type:'checkbox',align:"center"},
                {type:'numbers',title:"序号",align:"center"},
                {field:'userCode',title:'用户学号',align:"center"},
                {title:'用户照片',align:"center",height:"57px",templet:function (data) {
                        return "<img src="+'http://'+data.avatarUrl+">";
                    }},
                {field:'account',title:'用户账号',align:"center"},
                {field:'fullName',title:'真实姓名',align:"center"},
                {field:'studyDate',title:'学习时长',align:"center"},
                {field:'profession',title:'所属专业',align:"center",templet:function (data) {
                        if(data.profession==null||data.profession==""){
                            return "未选择";
                        }
                        return data.profession+"专业";
                    }},
                {field:'consumption',title:"消费金额",align:"center",templet:function (data){
                        return data.consumption+"元";
                    }},
                {field:'status',title:'账号状态',align:"center",templet:function (data) {
                        if(data.status=="2"){
                            var radio2 = '<a href="javascript:beforeUserAccount('+data.userId+','+data.status+');"><input type="checkbox" name="sate" lay-skin="switch" lay-text="正常|停用" lay-filter="switchTest" value="'+data.status+'" checked></a>';
                        }else if (data.status=="5") {
                            var radio2 = '<a href="javascript:beforeUserAccount('+data.userId+','+data.status+');"><input type="checkbox" name="sate" lay-skin="switch" lay-text="正常|停用" lay-filter="switchTest" value="'+data.status+'"></a>';
                        }
                        return radio2;
                    }},
                {title:'操作',align:"center",templet:function (data) {
                        return "<div>"
                            +"<a href='javascript:chakan("+data.userId+");' style='color: #2F4056;margin-right: 10px;'>查看</a>"
                            +"<a href='javascript:bianji("+data.userId+");' style='color: #2F4056;margin-right: 10px;'>编辑</a>"
                            +"<a href='javascript:fengting(\""+data.userId+"\",\""+data.account+"\",\""+data.status+"\");' style='color: #2F4056;margin-right: 10px;'>封停</a>"
                            +"</div>";
                    }}
            ]]
        })
    };

    //搜索
    $("#sousuo").click(function () {
        var account = $("#account").val();
        var nickName = $("#nickName").val();
        var createDate = $("#createDate").val();
        selectNoUser(account,nickName,createDate,0);
    });

    //重置
    $("#reset").click(function () {
        $("#account").val("");
        $("#nickName").val("");
        $("#createDate").val("");
        selectNoUser("","","",0);
    });

    //赠送优惠券
    $("#zengsong").click(function () {
        //监听表中数据
        var checkStatus = table.checkStatus("root").data;
        if(checkStatus.length > 0){
            checkStatus.forEach(function (data) {
                var obj;
                layer.open({
                    type:2,
                    shode:0,
                    maxmin:true,
                    title:"赠送优惠券",
                    content:"/coupon/coupon-give.html",
                    area:['800px','600px'],
                    btn:["关闭"],
                    yes:function (index) {
                        layer.close(index);
                    },success:function (layero,index) {
                        obj = layero.find('iframe').contents();
                        obj.find("#userId").val(data.userId)
                    }
                })
            })
        }else{
            layer.msg("必须选择一个用户");
        }
    });

    //更改账号状态
    beforeUserAccount = function beforeUserAccount(userId,status){
        if(status=="2"){
            var status1 = "5";
        }else{
            var status1 = "2";
        }
        var startDate = new Date();
        startDate = getTime(startDate);
        var endDate = new Date();
        endDate = getTimes(endDate,"365");
        var json = {
            userId:userIdd,
            id:userId,
            type:2,
            reason:"强制封停",
            startDatee:startDate,
            endDatee:endDate,
            days:36524,
            status:status1
        };
        $.ajax({
            type:"post",
            dataType:"post",
            data:json,
            url:"/beforeUser/beforeUserAccount",
            success:function (data) {
                if(data){
                    layer.msg("更改成功");
                }else{
                    layer.msg("更改失败");
                }
            }
        })
    };

    //查看
    chakan = function chakan(userId) {
        //console.log(userId);
        $.ajax({
            type:"post",
            dataType:"json",
            data:{"userId":userId},
            url:"/beforeUser/selectZoreUserById",
            success:function (data) {
                chakanLayer(data.data);
            }
        });
    };
    function chakanLayer(data){
        var obj;
        layer.open({
            type:2,
            shode:0,
            maxmin:true,
            title:"查看用户信息",
            content:"/user/before-user-see.html",
            area:['1000px','700px'],
            btn:["关闭"],
            yes:function (index) {
                layer.close(index);
            },success:function (layero,index) {
                obj = layero.find('iframe').contents();
                obj.find("#avatarUrl").attr("src",'http://'+data.avatarUrl);
                obj.find("#account").val(data.account);
                obj.find("#nickname").val(data.nickname);
                obj.find("#userCode").val(data.userCode);
                obj.find("#fullName").val(data.fullName);
                obj.find("#sex").val(data.sex);
                obj.find("#idCard").val(data.idCard);
                obj.find("#phone").val(data.phone);
                obj.find("#email").val(data.email);
                obj.find("#profession").val(data.profession);
                obj.find("#integration").val(data.integration);
                obj.find("#consumption").val(data.consumption);
                obj.find("#studyDate").val(data.studyDate);
                obj.find("#entranceDate").val(data.entranceDate);
                obj.find("#graduationDate").val(data.graduationDate);
                obj.find("#createDate").val(data.createDate);
                obj.find("#freestyle").val(data.freestyle);
            }
        })
    };

    //编辑
    bianji = function bianji(userId) {
        $.ajax({
            type:"post",
            dataType:"json",
            data:{"userId":userId},
            url:"/beforeUser/selectZoreUserById",
            success:function (data) {
                bianjiLayer(data.data);
            }
        });
    };
    function bianjiLayer(data){
        var obj;
        layer.open({
            type:2,
            shode:0,
            maxmin:true,
            title:"编辑用户信息",
            content:"/user/before-user-see-edit.html",
            area:['1000px','700px'],
            btn:["确定","关闭"],
            yes:function (index) {
                var json = {
                    userId:obj.find("#userId").val(),
                    avatarUrl:localStorage.getItem("mainImage"),
                    account:obj.find("#account").val(),
                    nickname:obj.find("#nickname").val(),
                    userCode:obj.find("#userCode").val(),
                    fullName:obj.find("#fullName").val(),
                    sex:obj.find("#sex").val(),
                    idCard:obj.find("#idCard").val(),
                    phone:obj.find("#phone").val(),
                    email:obj.find("#email").val(),
                    profession:obj.find("#profession").val(),
                    integration:obj.find("#integration").val(),
                    consumption:obj.find("#consumption").val(),
                    studyDate:obj.find("#studyDate").val(),
                    entranceDate:obj.find("#entranceDate").val(),
                    graduationDate:obj.find("#graduationDate").val(),
                    createDate:obj.find("#createDate").val(),
                    freestyle:obj.find("#freestyle").val()
                };

                $.ajax({
                    type:"post",
                    dataType:"json",
                    data:json,
                    url:"/beforeUser/updateBeforeUser",
                    success:function (data) {
                        if(data){
                            layer.msg("编辑成功");
                            layer.close(index);
                            selectNoUser("","","",0);
                        }else{
                            layer.msg("编辑失败");
                        }
                    }
                })
            },btn2:function(index){
                layer.close(index);
            },success:function (layero,index) {
                obj = layero.find('iframe').contents();
                obj.find("#userId").val(data.userId);
                obj.find("#avatarUrl").attr("src",'http://'+data.avatarUrl);
                obj.find("#account").val(data.account);
                obj.find("#nickname").val(data.nickname);
                obj.find("#userCode").val(data.userCode);
                obj.find("#fullName").val(data.fullName);
                obj.find("#sex").val(data.sex);
                obj.find("#idCard").val(data.idCard);
                obj.find("#phone").val(data.phone);
                obj.find("#email").val(data.email);
                selectLay(obj,data.profession,data.profession,"#profession");
                obj.find("#integration").val(data.integration);
                obj.find("#consumption").val(data.consumption);
                obj.find("#studyDate").val(data.studyDate);
                obj.find("#entranceDate").val(data.entranceDate);
                obj.find("#graduationDate").val(data.graduationDate);
                obj.find("#createDate").val(data.createDate);
                obj.find("#freestyle").val(data.freestyle);
            }
        })
    };
    //封停
    fengting = function fengting(userId,account,status) {
        if(status=="5"){
            layer.msg("此账号已被封停");
        }else{
            var obj;
            layer.open({
                type:2,
                shode:0,
                maxmin:true,
                title:"封停用户",
                content:"/user/user-no-jinsi-shutdown.html",
                area:['500px','370px'],
                btn:['确定','取消'],
                yes:function (index) {
                    var days = obj.find("#days").val();
                    var reason = obj.find("#reason").val();
                    var startDate = new Date();
                    startDate = getTime(startDate);
                    var endDate = new Date();
                    endDate = getTimes(endDate,days);
                    var status = "5";
                    var type = "3";
                    var id = userId;

                    if(days==""){
                        layer.msg("请选择封停天数");
                    } else if(reason=="null"||reason==""){
                        layer.msg("封停原因不能为空");
                    }else{
                        var json = {
                            userId:userIdd,
                            id:id,
                            type:type,
                            reason:reason,
                            startDatee:startDate,
                            endDatee:endDate,
                            days:days,
                            status:status
                        };
                        $.ajax({
                            type:"post",
                            dataType:"json",
                            data:json,
                            url:"/beforeUser/insertUserClosure",
                            success:function (data) {
                                if(data){
                                    layer.msg("封停成功");
                                    location.reload();
                                    layer.close(index);
                                }else{
                                    layer.msg("封停失败");
                                }
                            }
                        });
                    }
                },btn2:function (index) {
                    layer.close(index);
                },success:function (layero,index) {
                    obj = layero.find('iframe').contents();
                    obj.find("#account").val(account);
                }
            })
        }
    };


    //顶部时间
    function getTimes(date,days){
        var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
        var month = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
        var today = date.getDate(); //获取当前日(1-31)
        var hour = date.getHours(); //获取当前小时数(0-23)
        var minute = date.getMinutes(); //获取当前分钟数(0-59)
        var second = date.getSeconds(); //获取当前秒数(0-59)
        days = parseInt(days);
        today = parseInt(today);
        month = parseInt(month);
        year = parseInt(year);
        if(days=="1"){
            today = today+days;
            if(today>32|today>31){
                if(month=1|3|5|7|8|10|12){
                    month+=1;
                }else if(month=4|6|9|11){
                    month+=1;
                }else{
                    month+=1;
                }
            }
        }else if(days=="30"){
            month = month+1;
            if(month>13){
                year=year+1;
            }
        }else if(days=="365"){
            year=year+1;
        }else{
            year=year+100;
        }
        var nowTime;
        nowTime = year+'-'+fillZero(month)+'-'+fillZero(today)+' '+fillZero(hour)+':'+fillZero(minute)+':'+fillZero(second);
        return nowTime;
    };
    //顶部时间
    function getTime(date){
        var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
        var month = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
        var today = date.getDate(); //获取当前日(1-31)
        var hour = date.getHours(); //获取当前小时数(0-23)
        var minute = date.getMinutes(); //获取当前分钟数(0-59)
        var second = date.getSeconds(); //获取当前秒数(0-59)
        var nowTime;
        nowTime = year+'-'+fillZero(month)+'-'+fillZero(today)+' '+fillZero(hour)+':'+fillZero(minute)+':'+fillZero(second);
        return nowTime;
    };
    //时间的为零操作
    function fillZero(str){
        var realNum;
        if(str<10){
            realNum	= '0'+str;
        }else{
            realNum	= str;
        }
        return realNum;
    };
    //加入lay-value的下拉框矫正
    function selectLay(obj,layValue,value,select) {
        obj.find(select).children().each(function () {
            if ($(this).val() == layValue) {
                var status = 'dd[lay-value=' + layValue + ']';
                obj.find(select).siblings("div.layui-form-select").find('dl').find(status).click();
                $(this).parent().append('<option value="'+layValue+'" selected>' + value + '</option>');
            }
        })
    };

    //刷新
    $('#shuaxin').click(function () {
        location.reload();
    });

    //返回
    $("#goBack").click(function (s) {
        parent.$('#Jump').val('首页');
        parent.getQuick();
        return;
    });
});